package andy.ham;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import andy.ham.DaoMaster.DevOpenHelper;

//继承来自listView
public class LifeDiary extends ListActivity {

	// 插入一条新纪录
	public static final int MENU_ITEM_INSERT = Menu.FIRST;
	// 编辑内容
	public static final int MENU_ITEM_EDIT = Menu.FIRST + 1;

	/**创建读取数据库对象db、3个Dao框架相关对象以及存放数据的Cursor对象。*/

	private SQLiteDatabase db;

	/**daoMaster主要用于实例化数据库对象
	 * daoSession可以对数据库表进行增删改查
	 * noteDao主要用于对数据库中的数据进行增删改查
	 * 就算应用只需要使用noteDao对象，但是还是要按照 daoMaster -> daoSession -> noteDao的顺序初始化对象。*/

	private DaoMaster daoMaster;
	private DaoSession daoSession;
	private NoteDao noteDao;

	private Cursor cursor;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diary_list);

		// 初始化GreenDAO
		InitDAO();

		// 初始化List
		InitList();
	}

	/**
	 * DaoMaster类可以使用DevOpenHelper内部类方法初始化数据库对象，该方法有3个参数：
	 * 1. Content			窗口
	 * 2. String			数据库名
	 * 3. CursorFactory		游标cousor指向的行数
	 */
	private void InitDAO() {
		/**在当前Activity中打开名为notes-db的数据库，游标cursor为空，默认指向数据库第一行的数据
		 * 使用GreenDao框架定义的DevOpenhelper方法初始化数据库 “notes-db”，
		 * 然后赋值给SQLiteDatabase变量打开数据库，
		 * 再通过该变量依次初始化daoMaster、daoSession、noteDao对象以便使用DAO类对数据库进行操作*/

		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
				null);
		db = helper.getWritableDatabase();
		daoMaster = new DaoMaster(db);
		daoSession = daoMaster.newSession();
		noteDao = daoSession.getNoteDao();

	}

	/**
	 * 初始化List
	 * InitList()方法中，
	 * 首先在81~82行将获取的title、date的列名分别赋值给两个String对象，
	 * 第84行设置读取数据库的order，
	 * 第86~93行就是将读取出的数据显示在ListView中
	 */
	private void InitList() {
		String textColumn = NoteDao.Properties.Title.columnName;
		String dateColunm = NoteDao.Properties.Date.columnName;

		String orderBy = dateColunm + " COLLATE LOCALIZED DESC";

		cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(),
				null, null, null, null, orderBy);
		String[] from = { textColumn, dateColunm };
		int[] to = { R.id.text1, R.id.created };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.diary_row, cursor, from, to);
		setListAdapter(adapter);
	}

	// 添加选择菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_ITEM_INSERT, 0, R.string.menu_insert);
		return true;
	}

	// 添加选择菜单的选择事件
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			// 插入一条数据
			case MENU_ITEM_INSERT:
				Intent intent0 = new Intent(this, DiaryEditor.class);

				intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
				//查阅
				intent0.setData(getIntent().getData());
				startActivity(intent0);
				LifeDiary.this.finish();
				cursor.requery();
				return true;

		}
		return super.onOptionsItemSelected(item);
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {

		String mid = Long.toString(id);
		Log.d("id", mid);


		Intent intent = new Intent(this, DiaryEditor.class);
		intent.setAction(DiaryEditor.EDIT_DIARY_ACTION);
		Bundle bundle = new Bundle();
		bundle.putLong("id", id);
		intent.putExtras(bundle);
		Log.d("id", mid);
		startActivity(intent);
		LifeDiary.this.finish();
		cursor.requery();
	}

	protected void onActivityResult(int requestCode, int resultCode,
									Intent intent) {
		cursor.requery();
		super.onActivityResult(requestCode, resultCode, intent);
		// renderListView();
	}

}
